const Koa = require('koa')
const app = new Koa()
// socket
const server = require('http').Server(app.callback())
const io = require('socket.io')(server, { cors: true })
const port = 3000
server.listen(process.env.PORT || port, () => {
	console.log(`app run at : http://127.0.0.1:${port}`)
})
module.exports = io

// 静态资源中间件
const static = require('koa-static')
const path = require('path')
app.use(static(path.join(__dirname, 'public')))
// 访问图片地址 localhost:3009/upload/xx.png

// 解决跨域问题
const cors = require('koa2-cors')
app.use(cors())

// 对于POST请求的处理，koa-bodyparser中间件可以把koa2上下文的formData数据解析到ctx.request.body中
// ctx.request.body  获取post提交的数据
const bodyparser = require('koa-bodyparser')
app.use(bodyparser())

// 引入各路由模块
const classify = require('./modules/classify')
const dishes = require('./modules/dishes')
const staff = require('./modules/staff')
const tableCut = require('./modules/table-cut')
const tableManage = require('./modules/table-manage')
const table = require('./modules/table')
const user = require('./modules/user')
const order = require('./modules/order')
// 启动路由 设置response响应头
app.use(classify.routes(), classify.allowedMethods())
app.use(dishes.routes(), dishes.allowedMethods())
app.use(staff.routes(), staff.allowedMethods())
app.use(tableCut.routes(), tableCut.allowedMethods())
app.use(tableManage.routes(), tableManage.allowedMethods())
app.use(table.routes(), table.allowedMethods())
app.use(user.routes(), user.allowedMethods())
app.use(order.routes(), order.allowedMethods())

app.listen(3009)

console.log('项目启动成功,监听3009端口');
